<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/16
 * Time: 下午12:59
 */

namespace App\HttpController\Laser;


use App\HttpController\Common;
use App\Bean\Switchor;
use App\Model\Switchor as Model;
use EasySwoole\Core\Http\UrlParser;

class Index extends Common
{
    // 红外探测器实时数据返回
    public function sync() {
        $params = $this->request()->getQueryParams();
        $hard_id = $params['id'];
        $request_time = $params['time'];

        if (!$request_time) {
            $this->data_encode(null, 10002);
        }

        if (!$hard_id) {
            $this->data_encode(null, 10003);
        }

        $hard_id = intval(substr($hard_id, -2));
        if ($hard_id === 0) {
            $this->data_encode(null, 10004);
        }

        $model = new Model();
        $switch = $model->find($hard_id);


        $data = [
            'id'    => $params['id'],
            'time'  => $switch->getTime(),
            'status'=> $switch->getStatus()
        ];

        $this->data_encode($data);
    }

    // 单个设备历史数据返回
    public function multiple() {
        $params = $this->request()->getQueryParams();
        $hard_id = $params['id'];
        $start_time = $params['start'];
        $end_time = $params['end'];
        if (!$start_time)
            return $this->data_encode(null, 10005);
        if (!$end_time)
            $end_time = time();

        if ($start_time > $end_time)
            return $this->data_encode(null, 10006);

        if (!$hard_id) {
            $this->data_encode(null, 10003);
        }

        $hard_id = intval(substr($hard_id, -2));
        if ($hard_id === 0) {
            $this->data_encode(null, 10004);
        }
        
        $model = new Model();
        $list = $model->select($hard_id, $start_time, $end_time);
        if (!$list) return $this->data_encode(null, 10007);
        foreach ($list as $item)
            $data[] = [
                'id'    => $params['id'],
                'time'  => $item->getTime(),
                'status'=> $item->getStatus()
            ];
        $this->data_encode($data);
    }

    // 根据分组返回数据
}